package com.sfexpress.tools;

import org.apache.commons.lang.StringUtils;

import com.sfexpress.tools.entity.ConsCheckResult;
import com.sfexpress.tools.entity.MysqlTableInfo;
import com.sfexpress.tools.entity.ConsCheckResult.ItemResult;
import com.sfexpress.tools.utils.Pair;
import com.sfexpress.tools.entity.ConsCheckResult.ConflictItem;

/**
 * mysql表级信息一致性检查类
 * @author 01169238
 *
 */
public class MysqlTableInfoChecker extends AbstractChecker {
	
	public void checkConsistency(MysqlTableInfo pvTableInfo, MysqlTableInfo gvTableInfo, ConsCheckResult result) {
		
		logger.info("开始执行表信息一致性校验 ...");
		
		boolean flag = true;
		String key = "TABLE_DEFINITION";
		ItemResult<MysqlTableInfo> tbInfoCheckResult = result.getTbInfoCheckResult();
		if(!StringUtils.equals(pvTableInfo.getTableType(), gvTableInfo.getTableType())) {
			flag = false;
			tbInfoCheckResult.addConflictItem(key, new ConflictItem(key, "TABLE_TYPE", pvTableInfo.getTableType(), gvTableInfo.getTableType()));
		}
		if(!StringUtils.equals(pvTableInfo.getEngine(), gvTableInfo.getEngine())) {
			flag = false;
			tbInfoCheckResult.addConflictItem(key, new ConflictItem(key, "ENGINE", pvTableInfo.getEngine(), gvTableInfo.getEngine()));
		}
		
		if(!flag) {
			logger.warn("检测到表信息不一致 : ");
			logger.warn("From 生产版本库 : " + pvTableInfo);
			logger.warn("From 灰度版本库 : " + gvTableInfo);
			result.getTbInfoCheckResult().setCoincident(flag);
			result.getTbInfoCheckResult().getConflictItems().add(new Pair<MysqlTableInfo, MysqlTableInfo>(pvTableInfo, gvTableInfo));
		}
		
		logger.info("表信息一致性校验结束!");
		
	}

}
